#include "Configuration.h"
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <utility>

Configuration::Configuration(const std::string & filePath) 
    : m_filePath(filePath)
{
    initconfigMap();
    initstopWordList();
} 

Configuration::~Configuration() { }



// 获取配置文件内容
std::map<std::string, std::string> &
Configuration::getconfigMap() {
    return m_configMap;
}

// 获取停用词集合
std::set<std::string> &
Configuration::getstopWordList() {
    return m_stopWordList;
}



// 初始化配置文件内容
void Configuration::initconfigMap() {
    std::ifstream   file(m_filePath);
    if (! file.good()) {
        std::cerr << "[error]: open " << m_filePath << "error!" << std::endl;
        return;
    }

    file.seekg(0);
    std::string     text;
    while (std::getline(file, text)) {
        std::stringstream   line(text);
        std::string         key, value;

        while (line >> key >> value) {
            m_configMap.insert(std::pair<std::string, std::string>(key, value));
        }
    }

    file.close();
}

// 初始化停用词集合
void Configuration::initstopWordList() {
    std::string     Path = m_configMap[m_configArgs.STOP_WORD_PATH];
    std::ifstream   file(Path);
    if (! file.good()) {
        std::cerr << "[error]: open " << Path << "error!" << std::endl;
        return;
    }

    file.seekg(0);
    std::string     stopWord;
    while (file >> stopWord) {
        m_stopWordList.insert(stopWord);
    }

    file.close();
}